import { Component, OnInit } from '@angular/core';
import { JsonApiService } from "app/core/api/json-api.service"; //http请求配置引入
import { Router, ActivatedRoute } from "@angular/router"; //路由引入
import { config } from '../../shared/smartadmin.config';
declare var $: any;
@Component({
  selector: 'sa-project-nav',
  templateUrl: './project-nav.component.html'
})
export class ProjectNavComponent implements OnInit {
  tableData = []; //table数据
  totalCount: number = 0; //分页总条数
  curPage: number = 1; //分页当前页 
  pageSize: number = 10; //内置的分页每页条数
  isEditStatus: boolean = false; // 判断是否修改状态
  table: any = {
    projectType: '',
    type: '',
    searchKey: ''
  }
  projectFrom: any = {
    projectType: 1,
    type: 1,
    id: '',
    address: '',
    userName: '',
    password: '',
    linkMan: '',
    description: '',
    sort: '',
    navigationName: '',
  }
  projectType = {
    1: 'GPS流向管控平台',
    2: '流向管控平台',
    3: '汉德管理平台',
    4: '第三方平台'
  }
  type: any = {
    0: '其他',
    1: '通用',
    2: '定制化'
  }
  constructor(
    private jsonApiService: JsonApiService,
    private router: Router,
    private activatedRoute: ActivatedRoute
  ) {
    activatedRoute.queryParams.subscribe(queryParams => {
      this.curPage = queryParams.nowPage || 1;
    });
  }
  ngOnInit() {
    this.load();
  }
  //table刷新
  refresh() {
    this.load();
  }
  //表格搜索
  table_search() {
    this.curPage = 1;
    this.load();
  }
  // 项目类型回调
  projectTypeChange() {
    this.table.projectType = '';
  }

  // 类别回调
  typeChange() {
    this.table.type = ''
  }


  //初始化加载list数据
  load() {
    let data = {
      pageSize: this.pageSize,
      nowPage: this.curPage,
      parameters: {
        projectType: this.table.projectType,
        type: this.table.type,
        searchKey: common.stringTrim(this.table.searchKey),
      }
    }
    let reqdata = 'gridPager=' + JSON.stringify(data);
    this.jsonApiService.http("projectNavigation/pageProjectNavigation.html", reqdata).subscribe(data => {
      this.tableData = data.exhibitDatas;
      this.totalCount = data.recordCount;
    })
  }

  // 新增导航
  add_nav(modal) {
    this.resetForm();
    modal.show();
    this.isEditStatus = false;
  }

  // 编辑数据
  editRow(row, modal) {
    console.log("editRow", row)
    this.isEditStatus = true
    modal.show();
    this.projectFrom.projectType = row.projectType;
    this.projectFrom.id = row.id;

    this.projectFrom.type = row.type;
    this.projectFrom.address = row.address;
    this.projectFrom.userName = row.userName;
    this.projectFrom.password = row.password;
    this.projectFrom.linkMan = row.linkMan;
    this.projectFrom.description = row.description;
    this.projectFrom.sort = row.sort;
    this.projectFrom.navigationName = row.navigationName;
  }

  // 删除数据

  deleteRow(row) {
    this.jsonApiService.confirm_message("此操作将永久删除数据,确认要删除?").then(() => {
      var reqdata = "id=" + row.id;
      this.jsonApiService.http('projectNavigation/delProjectNavigation.html', reqdata).subscribe(result => {
        this.jsonApiService.message(result.message);
        this.refresh();
      })
    })
  }
  //分页回调
  paginate(e) {
    this.curPage = e.page + 1;
    this.pageSize = e.rows;
    this.load();
  }
  // resetForm
  resetForm() {
    this.projectFrom = {

      projectType: 1,
      type: 1,
      id: '',
      address: '',
      userName: '',
      password: '',
      linkMan: '',
      description: '',
      sort: '',
      navigationName: '',
    }
  }

  /*
  * 关闭弹窗
  */
  modalClose(modal) {
    modal.hide()
    this.resetForm();
  }

  /*
  * form提交
  */
  apiControlSubmit(modal) {
    if (!this.projectFrom.address) {
      this.jsonApiService.message('请填写项目地址')
      return
    }
    if (!this.projectFrom.userName) {
      this.jsonApiService.message('请填写账号')
      return
    }
    if (!this.projectFrom.password) {
      this.jsonApiService.message('请填写密码')
      return
    }
    if (!this.projectFrom.navigationName) {
      this.jsonApiService.message('导航名称')
      return
    }

    if (!this.projectFrom.address.startsWith('http://') && !this.projectFrom.address.startsWith('https://')) {
      this.jsonApiService.message('地址开头请以http或https开头')
      return
    }
    try {
      this.isEditStatus ? '' : delete this.projectFrom.id
    } catch { }
    const url = this.isEditStatus ? 'projectNavigation/editProjectNavigation.html' : 'projectNavigation/addProjectNavigation.html'
    this.jsonApiService.http_obj(url, this.projectFrom).subscribe(data => {
      this.jsonApiService.message(data.message)
      if (data.success) {
        this.load()
        this.modalClose(modal)
      }
    })
  }
}